/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","msgServ", "ajaxServ", "formServ", "bizProto","btnServ","commonServ"],
		function($,datagridServ, msgServ, ajaxServ, formServ, bizProto,btnServ,commonServ) {

			'use strict';

			var _bizProto,_buttonConfig,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,				
				//设置密码弹窗对象及按钮对象
				$password_window,password_btn,
				//业务表单对象
				$form,
				//部门选择框字段
				
				//本地数据库密码设置路径
				basePasswordUrl = '/user/securityUserPasswordReset.action',passwordSaveUrl,
				
				//外部数据库密码设置路径
				outpointPasswordUrl = '/outPointPassword/resetPassword.action',passwordOutpointSaveUrl,
				
				baseQueryUrl = "/user/getListDomain.action",userQueryUrl,
				
				rootid = 'panel-user',
				
				window_id ='userpassword-win',
				
				//脚本调用参数缓存
				scriptParam = null				

			var public_method = {
				
				/**
				 * 列表页面初始化方法
				 */
				init:function(doc){
					private_method.init(doc);
				}
				,
				/**
				 * 直接弹窗初始化方法
				 * @param _options.user	当前用户
				 * @param _options.rootPath	url根路径
				 */
				popInit:function(_options){
					private_method.popInit(_options);
				}
				
			}

			var private_method = {
				
				/**
				 * 直接弹窗初始化方法
				 * @param _options.user	当前用户
				 * @param _options.rootPath	url根路径
				 */
				popInit	: function(_options){
					
					scriptParam = _options
					//弹出窗口
					private_method.popWindow();					
					
					
				},
				
				//弹出窗口
				popWindow:function(){
					var $password_window = $('#userpassword-win');
					var _default = {
						width:400,
						height:300,						
						modal:true,
						closable:true,
						title	: '修改密码',
						href	: '/honest/user/userPasswordPage.action',
						onClose	: private_method.close
					}
					
					$($password_window).window(_default);
					
					$($password_window).window('open').window('vcenter').window('hcenter')	
				},
				
				init : function(doc) {
					if (window.console) {
					console.info('userPassword modules gets ready and been loaded.');
					}
					_bizProto = new bizProto({doc:doc,rootId:rootid,panelSelector:'div.well'})
					
					
					private_method.initPath();
					private_method.initElement()
				},
				
				//初始化路径
				initPath:function(){
					
					var _rootPath
					
					if(scriptParam && scriptParam.rootPath){
						_rootPath = scriptParam.rootPath
					}else{
						_rootPath = _bizProto.rootPath
					}					
					
					passwordSaveUrl = _rootPath +basePasswordUrl;		
					
					passwordOutpointSaveUrl = _rootPath + outpointPasswordUrl;
				},
				
				
				/**
				 * 初始化元素
				 */
				initElement:function(){
					
					
					$grid = $('#'+rootid +' div.dg-user');					
					// 编辑窗口初始化
					$password_window = $('#userpassword-win')
					password_btn = $($password_window).find('a.function-btn')
					$(password_btn).unbind('click')
					$(password_btn).bind('click',	private_method.function_btn_listenner)
					$form = $('#'+window_id + ' form')					
					
					var selectedRecord
					if(scriptParam && scriptParam.user){
						selectedRecord = scriptParam.user
					}else{
						selectedRecord =$grid.datagrid('getSelected');
						if(selectedRecord.parentid==null){
							selectedRecord.parentid='';
						}
					}
					$form.form('load',selectedRecord);

				},
				//功能按钮监听函数
				function_btn_listenner:function(e){
					
					_buttonConfig={						
						 "保存密码":private_method.savePassword,
						 "取消":private_method.cancel									
					}
					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
					
				},
				
				// 提交form
				savePassword : function(e) {
					
					var password_form = $('#'+window_id + ' form')					
					
					var _resetParam = {
						form	: $form,
						grid	: $grid,
						url		: passwordSaveUrl,
						callback: private_method.saveback
					}
					
					//获取密码重置配置项，确定执行本地或外部密码重置后台服务
					var resetType = commonServ.getConfig({configName:'resetPasswordType'});
					if(resetType){
						_resetParam.url = passwordOutpointSaveUrl;
					}					
					
					datagridServ.save(_resetParam)
					
					if(e &&　e.currentTarget){
						$(e.currentTarget).linkbutton('enable');
					}
				},			
				saveback:function(data){
					if(data && data.data){
						if(data.data.status=='success'){
							msgServ.Msgslide('密码设置成功!')
						}else{
							msgServ.Msgslide('密码设置失败!')
						}
					}
				},
				//
				cancel : function(e) {
					if(e &&　e.currentTarget){
						$(e.currentTarget).linkbutton('enable');
					}
					$('#'+window_id).window('close');
				}
				,
				close	: function(){
					scriptParam = null
				}

			};

			
			return public_method;

		});